package com.mingtu.jgspatrol.activity;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.activity.NaviMapActivity;
import com.mingtu.common.base.BaseApplication;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.ConvertLatlng;
import com.mingtu.common.utils.GridSpacingItemDecoration;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.adapter.FirePictureVideoAdapter;
import com.mingtu.jgspatrol.base.MyBaseActivity;
import com.mingtu.jgspatrol.bean.FireTaskBean;
import com.mingtu.jgspatrol.databinding.ActivityFireTaskDetailsBinding;
import com.mingtu.jgspatrol.utils.MyConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FireTaskDetailsActivity extends MyBaseActivity<ActivityFireTaskDetailsBinding> {
    private String taskId;
    private String stype;
    private List<FireTaskBean.DataBean.FireInfoEntityBean.FireInfoAttachEntityListBean> ReportDataBean = new ArrayList<>();
    private List<FireTaskBean.DataBean.FireInfoEntityBean.FireInfoAttachEntityListBean> SureDataBean = new ArrayList<>();
    private String createTime;
    private FirePictureVideoAdapter firePictureVideoAdapter1;
    private FirePictureVideoAdapter firePictureVideoAdapter2;
    private ArrayList<LocalMedia> selectPictureList1;
    private ArrayList<LocalMedia> selectPictureList2;

    private String lngStr;
    private String latStr;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fire_task_details;
    }

    @Override
    protected void initView() {
        setModuleTitle("任务详情");
    }

    @Override
    protected void initData() {
        taskId = getIntent().getStringExtra("taskId");
        stype = getIntent().getStringExtra("type");
        if (!StringUtils.isEmpty(taskId)) {
            getTaskDetails();
        } else {
            ToastUtils.showLong(getResources().getString(R.string.data_error));
        }


        GridLayoutManager layoutManager1 = new GridLayoutManager(context, 3);//第二个参数为网格的列数
        binding.reportPictureVideo.addItemDecoration(new GridSpacingItemDecoration(3, SizeUtils.dp2px(15), false));
        binding.reportPictureVideo.setLayoutManager(layoutManager1);

        firePictureVideoAdapter1 = new FirePictureVideoAdapter(context, ReportDataBean);
        binding.reportPictureVideo.setAdapter(firePictureVideoAdapter1);
        firePictureVideoAdapter1.setOnItemClickListener(new FirePictureVideoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                BaseApplication.initPreviewModel().startActivityPreview(position, false, selectPictureList1);
            }
        });


        GridLayoutManager layoutManager = new GridLayoutManager(context, 3);//第二个参数为网格的列数
        binding.surePictureVideo.addItemDecoration(new GridSpacingItemDecoration(3, SizeUtils.dp2px(15), false));
        binding.surePictureVideo.setLayoutManager(layoutManager);

        firePictureVideoAdapter2 = new FirePictureVideoAdapter(context, SureDataBean);
        binding.surePictureVideo.setAdapter(firePictureVideoAdapter2);
        firePictureVideoAdapter2.setOnItemClickListener(new FirePictureVideoAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                BaseApplication.initPreviewModel().startActivityPreview(position, false, selectPictureList2);
            }
        });
        addClickListener(this, binding.layoutFireLocation);


    }

    @Override
    protected void getData() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.layout_fire_location:
                Bundle bundle = new Bundle();
                bundle.putString("lng", lngStr);
                bundle.putString("lat", latStr);
                IntentUtils.getInstance().readyGo(FireTaskDetailsActivity.this, NaviMapActivity.class, bundle);
                break;
        }
    }


    /**
     * 火情任务待确认详情
     */
    private void getTaskDetails() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id", taskId);
        hashMap.put("type", stype);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_SUPER_DETAILS)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            FireTaskBean fireTaskBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                fireTaskBean = new FireTaskBean();
                                fireTaskBean = gson.fromJson(result, FireTaskBean.class);

                                FireTaskBean.DataBean infoBean = fireTaskBean.getData();
                                binding.reportPictureVideo.setVisibility(View.GONE);
                                binding.layoutSurePic.setVisibility(View.GONE);
                                binding.surePictureVideo.setVisibility(View.GONE);
                                if (infoBean != null) {
                                    String name = infoBean.getName();
                                    createTime = infoBean.getCreateTime();
                                    String finishTime = infoBean.getFinishTime();
                                    FireTaskBean.DataBean.FireInfoEntityBean fireInfoEntityBean = infoBean.getFireInfoEntity();

                                    if (!StringUtils.isEmpty(name)) {
                                        binding.tvTaskTitle.setText(name);
                                    }

                                    if (!StringUtils.isEmpty(createTime)) {
                                        binding.tvStartTime.setText(createTime);
                                    }

                                    if (!StringUtils.isEmpty(finishTime)) {
                                        binding.tvEndTime.setText(finishTime);
                                    }

                                    if (fireInfoEntityBean != null) {
                                        String gradeType = fireInfoEntityBean.getGradeType(); //火情等级
                                        String fireType = fireInfoEntityBean.getFireType(); //火情类型
                                        String fireStatus = fireInfoEntityBean.getFireStatus(); //火情状态
                                        String resourceType = fireInfoEntityBean.getResourceType(); //资源类型
                                        String memo = fireInfoEntityBean.getMemo(); //火情备注

                                        if (!StringUtils.isEmpty(gradeType)) {
                                            binding.tvFireLevel.setText(gradeType);
                                        }
                                        if (!StringUtils.isEmpty(fireType)) {
                                            binding.tvFireType.setText(fireType);
                                        }
                                        if (!StringUtils.isEmpty(fireStatus)) {
                                            binding.tvFireStatus.setText(fireStatus);
                                        }
                                        if (!StringUtils.isEmpty(resourceType)) {
                                            binding.tvResourceType.setText(resourceType);
                                        }

                                        if (!StringUtils.isEmpty(memo)) {
                                            binding.tvFireMemo.setText(memo);
                                        }

                                        String uploadType = fireInfoEntityBean.getUploadType();
                                        String warningTime = fireInfoEntityBean.getCreateTime();
                                        String fireAddress = fireInfoEntityBean.getAddress();
                                        String lnglat = fireInfoEntityBean.getLnglat();
                                        String gridId = fireInfoEntityBean.getGridId();
                                        String deptName = fireInfoEntityBean.getDeptName();
                                        String manager = fireInfoEntityBean.getManager();
                                        String mobile = fireInfoEntityBean.getMobile();

                                        List<FireTaskBean.DataBean.FireInfoEntityBean.FireInfoAttachEntityListBean>
                                                listBeans = fireInfoEntityBean.getFireInfoAttachEntityList();


                                        if (lnglat.indexOf(",") != -1) {
                                            String[] lnglatArray = lnglat.split(",");
                                            lngStr = lnglatArray[0];
                                            latStr = lnglatArray[1];
                                            double lat = Double.parseDouble(latStr);
                                            double lng = Double.parseDouble(lngStr);
                                            String myLng = ConvertLatlng.getInstance().convertToSexagesimal(lng);
                                            String myLat = ConvertLatlng.getInstance().convertToSexagesimal(lat);
                                            binding.tvFireLngLat.setText(myLng + "," + myLat);
                                        }
                                        if (!StringUtils.isEmpty(uploadType)) {
                                            binding.tvWarningType.setText(uploadType);
                                        }
                                        if (!StringUtils.isEmpty(warningTime)) {
                                            binding.tvWarningTime.setText(warningTime);
                                        }
                                        if (!StringUtils.isEmpty(fireAddress)) {
                                            binding.tvFireLocation.setText(fireAddress);
                                        }

                                        if (!StringUtils.isEmpty(gridId)) {
                                            binding.tvMapGrid.setText(gridId);
                                        }
                                        if (!StringUtils.isEmpty(deptName)) {
                                            binding.tvDeptName.setText(deptName);
                                        }
                                        if (!StringUtils.isEmpty(manager)) {
                                            binding.tvPerson.setText(manager);
                                        }
                                        if (!StringUtils.isEmpty(mobile)) {
                                            binding.tvPersonPhone.setText(mobile);
                                        }

                                        if (listBeans != null) {
                                            if (listBeans.size() > 0) {
                                                ReportDataBean = new ArrayList<>();
                                                SureDataBean = new ArrayList<>();
                                                for (int i = 0; i < listBeans.size(); i++) { //将现场照片取出来
                                                    String type = listBeans.get(i).getType();  //1:现场照片   2：确认照片
                                                    if (type.equals("1")) {
                                                        ReportDataBean.add(listBeans.get(i));
                                                    } else if (type.equals("2")) {
                                                        SureDataBean.add(listBeans.get(i));
                                                    }
                                                }

                                                //现场照片逻辑
                                                if (ReportDataBean.size() > 0) {
                                                    binding.reportPictureVideo.setVisibility(View.VISIBLE);
                                                    selectPictureList1 = new ArrayList<>();
                                                    lookPicVideoEvent(ReportDataBean, selectPictureList1, firePictureVideoAdapter1);

                                                }

                                                //确认照片逻辑
                                                if (SureDataBean.size() > 0) {
                                                    binding.surePictureVideo.setVisibility(View.VISIBLE);
                                                    binding.layoutSurePic.setVisibility(View.VISIBLE);
                                                    selectPictureList2 = new ArrayList<>();
                                                    lookPicVideoEvent(SureDataBean, selectPictureList2, firePictureVideoAdapter2);
                                                }
                                            }
                                        }
                                    }

                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }

                        }
                    }
                });
    }

    /**
     * @param selectPictureList
     */
    private void lookPicVideoEvent(
            List<FireTaskBean.DataBean.FireInfoEntityBean.FireInfoAttachEntityListBean> DataBean,
            List<LocalMedia> selectPictureList,
            FirePictureVideoAdapter firePictureVideoAdapter) {

        for (int i = 0; i < DataBean.size(); i++) {
            String url = DataBean.get(i).getUrl();
            String resType = "1";  //1:图片   3：视频
            boolean isHasVideo = PictureMimeType.isUrlHasVideo(url); //现在只有图片和视频，如果添加音频文件的话，此处逻辑要改
            if (isHasVideo) {
                resType = "3";
            }
            //设置资源类型，在adapter 中使用
            DataBean.get(i).setType(resType);
            LocalMedia localMedia = LocalMedia.generateHttpAsLocalMedia(url);
            selectPictureList.add(localMedia);
        }
        firePictureVideoAdapter.upData(DataBean);
    }

}